﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Framework.Comum.DAL
{
    public static class ExtensaoClasseDados
    {
        public static T Copiar<T>(this object source, object objBD) where T:class, new()
        {
            T retorno = Activator.CreateInstance<T>();
            Type tipo = retorno.GetType();

            object valor = null;

            foreach (PropertyInfo prop_loopVariable in tipo.GetProperties())
            {
                PropertyInfo prop = prop_loopVariable;

                if (((!object.ReferenceEquals(prop.PropertyType, typeof(string))) && (prop.PropertyType.IsClass || prop.DeclaringType != tipo)))
                    continue;


                valor = prop.GetValue(objBD, null);

                prop.SetValue(retorno, valor, null);
            }

            return retorno;
        }
    }
}
